﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MS.Core.Base.Transaction;
using LHS.Core.TableInfo;
using MS.Core.Constants.Objects;
using System.Collections.Specialized;
using MS.Core.Base.Data;

namespace LHS.Win
{
    public partial class frmTienIch : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        public frmTienIch()
        {
            InitializeComponent();
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void frmTienIch_Load(object sender, EventArgs e)
        {
            this.loadData(this.gdThuThuat, InfoTreatmentSkill.TABLENAME, InfoTreatmentSkill.TRSSKILLNAME, InfoFacility.eFacilityType._none);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Value1"></param>
        /// <param name="strTableName"></param>
        /// <param name="strColumnKey"></param>
        /// <param name="Value5"></param>
        /// <param name="Value6">dung cho tab Phau thuat: moi them column Chu viet tat cua phau thuat 20090716</param>
        private void loadData(DevExpress.XtraGrid.GridControl Value1, string strTableName, string ColNameSort, InfoFacility.eFacilityType eFacilityType)
        {
            try
            {
                ListDictionary orderby = new ListDictionary();
                orderby.Add(ColNameSort, "asc");
                DataTable dt;
                //where them dieu kien type
                if (eFacilityType != InfoFacility.eFacilityType._none)
                {
                    List<ExpressionObject> param = new List<ExpressionObject>();
                    param.Add(new ExpressionObject(InfoFacility.FACTYPE, (int)eFacilityType));
                    dt = objController.GetData(strTableName, param, orderby);
                }
                else
                {
                    dt = objController.GetData(strTableName, null, orderby);
                }

                dt.Columns.Add(DBBaseInfo.Hidden_Status, typeof(int));
                dt.Columns.Add("Hidden_STT", typeof(int));
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i][DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._HaveValue;
                        dt.Rows[i]["Hidden_STT"] = i + 1;
                    }
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);

                Value1.DataSource = dt;
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }

        #region Thu Thuat
        private void gvThuThuat_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gdThuThuat.DataSource != null)
            {
                DataRow dr = gvThuThuat.GetDataRow(e.RowHandle);
                if (dr != null)
                {
                    if (Convert.ToInt16(dr[DBBaseInfo.Hidden_Status].ToString()) != (int)InfoClinicalPicture.eHiddenStatus._Register)
                        dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Updated;
                }
            }
        }

        private void gdThuThuat_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (gdThuThuat.DataSource == null)
            {
                e.Handled = true;
                return;
            }

            if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Append)
            {
                e.Handled = true;
                DataTable dt = (DataTable)gdThuThuat.DataSource;
                DataRow dr = dt.NewRow();
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Register;

                dt.Rows.Add(dr);
            }
            else if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Remove)
            {
                e.Handled = true;
                DataRow drFocus = gvThuThuat.GetDataRow(gvThuThuat.FocusedRowHandle);
                if (drFocus == null)
                {
                    e.Handled = true;
                    return;
                }
                objController.DeleteRow(drFocus, new string[] { InfoTreatmentSkill.TRSISN });
                drFocus.Delete();
                drFocus.AcceptChanges();
            }
        }

        private void btLuuTT_Click(object sender, EventArgs e)
        {
            if (gdThuThuat.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)gdThuThuat.DataSource;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Register).ToString())
                {
                    objController.InsertRow(dr, new string[] { InfoTreatmentSkill.TRSISN });
                }
                else if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Updated).ToString())
                {
                    objController.UpdateRow(dr, new string[] { InfoTreatmentSkill.TRSISN });
                }
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._HaveValue;
            }
            MessageBox.Show("Đã cập nhật thành công", "Thông Báo");
        }
        #endregion

        #region Phau Thuat
        private void gvPhauThuat_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gdPhauThuat.DataSource != null)
            {
                DataRow dr = gvPhauThuat.GetDataRow(e.RowHandle);
                if (dr != null)
                {
                    if (Convert.ToInt16(dr[DBBaseInfo.Hidden_Status].ToString()) != (int)InfoClinicalPicture.eHiddenStatus._Register)
                        dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Updated;
                }
            }
        }

        private void gdPhauThuat_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (gdPhauThuat.DataSource == null)
            {
                e.Handled = true;
                return;
            }

            if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Append)
            {
                e.Handled = true;
                DataTable dt = (DataTable)gdPhauThuat.DataSource;
                DataRow dr = dt.NewRow();
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Register;

                dt.Rows.Add(dr);
            }
            else if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Remove)
            {
                e.Handled = true;
                DataRow drFocus = gvPhauThuat.GetDataRow(gvPhauThuat.FocusedRowHandle);
                if (drFocus == null)
                {
                    e.Handled = true;
                    return;
                }
                objController.DeleteRow(drFocus, new string[] { InfoSurgical.SURISN });
                drFocus.Delete();
                drFocus.AcceptChanges();
            }
        }

        private void btLuuPT_Click(object sender, EventArgs e)
        {
            if (gdPhauThuat.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)gdPhauThuat.DataSource;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Register).ToString())
                {
                    objController.InsertRow(dr, new string[] { InfoSurgical.SURISN });
                }
                else if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Updated).ToString())
                {
                    objController.UpdateRow(dr, new string[] { InfoSurgical.SURISN });
                }
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._HaveValue;
            }
            MessageBox.Show("Đã cập nhật thành công", "Thông Báo");
        }
        #endregion

        #region Loi Khuyen
        private void gvLoiKhuyen_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gdLoiKhuyen.DataSource != null)
            {
                DataRow dr = gvLoiKhuyen.GetDataRow(e.RowHandle);
                if (dr != null)
                {
                    if (Convert.ToInt16(dr[DBBaseInfo.Hidden_Status].ToString()) != (int)InfoClinicalPicture.eHiddenStatus._Register)
                        dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Updated;
                }
            }
        }

        private void gdLoiKhuyen_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (gdLoiKhuyen.DataSource == null)
            {
                e.Handled = true;
                return;
            }

            if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Append)
            {
                e.Handled = true;
                DataTable dt = (DataTable)gdLoiKhuyen.DataSource;
                DataRow dr = dt.NewRow();
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Register;

                dt.Rows.Add(dr);
            }
            else if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Remove)
            {
                e.Handled = true;
                DataRow drFocus = gvLoiKhuyen.GetDataRow(gvLoiKhuyen.FocusedRowHandle);
                if (drFocus == null)
                {
                    e.Handled = true;
                    return;
                }
                objController.DeleteRow(drFocus, new string[] { InfoAdvice.ADVISN });
                drFocus.Delete();
                drFocus.AcceptChanges();
            }
        }

        private void btLuuLK_Click(object sender, EventArgs e)
        {
            if (gdLoiKhuyen.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)gdLoiKhuyen.DataSource;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Register).ToString())
                {
                    objController.InsertRow(dr, new string[] { InfoAdvice.ADVISN });
                }
                else if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Updated).ToString())
                {
                    objController.UpdateRow(dr, new string[] { InfoAdvice.ADVISN });
                }
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._HaveValue;
            }
            MessageBox.Show("Đã cập nhật thành công", "Thông Báo");
        }
        #endregion

        #region the rest consists of Tien Can, Benh Su, Nghe Nghiep, Dien Tien Benh
        private void gvTheRest_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DevExpress.XtraGrid.GridControl grid  = (DevExpress.XtraGrid.GridControl)((DevExpress.XtraGrid.Views.Grid.GridView)sender).GridControl;
            if (grid.DataSource != null)
            {
                DataRow drFocus = null;
                if (grid.Name == gdBenhSu.Name)
                {
                    drFocus = gvBenhSu.GetDataRow(e.RowHandle);
                }
                else if (grid.Name == gdDienTienBenh.Name)
                {
                    drFocus = gvDienTienBenh.GetDataRow(e.RowHandle);
                }
                else if (grid.Name == gdTienCan.Name)
                {
                    drFocus = gvTienCan.GetDataRow(e.RowHandle);
                }
                else if (grid.Name == gdNgheNghiep.Name)
                {
                    drFocus = gvNgheNghiep.GetDataRow(e.RowHandle);
                }
                if (drFocus != null)
                {
                    if (Convert.ToInt16(drFocus[DBBaseInfo.Hidden_Status].ToString()) != (int)InfoClinicalPicture.eHiddenStatus._Register)
                        drFocus[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Updated;
                }
            }
        }

        private void gdTheRest_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            DevExpress.XtraGrid.GridControl grid = (DevExpress.XtraGrid.GridControl)((DevExpress.XtraGrid.GridControlNavigator)sender).NavigatableControl;
            if (grid.DataSource == null)
            {
                e.Handled = true;
                return;
            }

            if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Append)
            {
                e.Handled = true;
                DataTable dt = (DataTable)grid.DataSource;
                DataRow dr = dt.NewRow();
                if (grid.Name == gdBenhSu.Name)
                {
                    dr[InfoFacility.FACTYPE] = (int)InfoFacility.eFacilityType._eBenhSu;
                }
                else if (grid.Name == gdDienTienBenh.Name)
                {
                    dr[InfoFacility.FACTYPE] = (int)InfoFacility.eFacilityType._eDienTienBenh;
                }
                else if (grid.Name == gdTienCan.Name)
                {
                    dr[InfoFacility.FACTYPE] = (int)InfoFacility.eFacilityType._eTienCan;
                }
                else if (grid.Name == gdNgheNghiep.Name)
                {
                    dr[InfoFacility.FACTYPE] = (int)InfoFacility.eFacilityType._eNgheNghiep;
                }
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._Register;

                dt.Rows.Add(dr);
            }
            else if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Remove)
            {
                e.Handled = true;
                DataRow drFocus = null;
                if (grid.Name == gdBenhSu.Name)
                {
                    drFocus = gvBenhSu.GetDataRow(gvBenhSu.FocusedRowHandle);
                }
                else if (grid.Name == gdDienTienBenh.Name)
                {
                    drFocus = gvDienTienBenh.GetDataRow(gvDienTienBenh.FocusedRowHandle);
                }
                else if (grid.Name == gdTienCan.Name)
                {
                    drFocus = gvTienCan.GetDataRow(gvTienCan.FocusedRowHandle);
                }
                else if (grid.Name == gdNgheNghiep.Name)
                {
                    drFocus = gvNgheNghiep.GetDataRow(gvNgheNghiep.FocusedRowHandle);
                }
                if (drFocus == null)
                {
                    e.Handled = true;
                    return;
                }
                objController.DeleteRow(drFocus, new string[] { InfoFacility.FACISN });
                drFocus.Delete();
                drFocus.AcceptChanges();
            }
        }

        /// <summary>
        /// luu cho nhung phan nhu tien can, dien bien benh, nghe nghiep, benh su
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btLuuTheRest_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            if (((Button)sender).Name == btnLuuBenhSu.Name)
            {
                if (gdBenhSu.DataSource == null)
                {
                    return;
                }
                dt = (DataTable)gdBenhSu.DataSource;
            }
            if (((Button)sender).Name == btnLuuDTBenh.Name)
            {
                if (gdDienTienBenh.DataSource == null)
                {
                    return;
                }
                dt = (DataTable)gdDienTienBenh.DataSource;
            }
            if (((Button)sender).Name == btnLuuNgheNghiep.Name)
            {
                if (gdNgheNghiep.DataSource == null)
                {
                    return;
                }
                dt = (DataTable)gdNgheNghiep.DataSource;
            }
            if (((Button)sender).Name == btnLuuTienCan.Name)
            {
                if (gdTienCan.DataSource == null)
                {
                    return;
                }
                dt = (DataTable)gdTienCan.DataSource;
            }
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Register).ToString())
                {
                    objController.InsertRow(dr, new string[] { InfoFacility.FACISN });
                }
                else if (dr[DBBaseInfo.Hidden_Status].ToString() == ((int)InfoClinicalPicture.eHiddenStatus._Updated).ToString())
                {
                    objController.UpdateRow(dr, new string[] { InfoFacility.FACISN });
                }
                dr[DBBaseInfo.Hidden_Status] = (int)InfoClinicalPicture.eHiddenStatus._HaveValue;
            }
            MessageBox.Show("Đã cập nhật thành công", "Thông Báo");
        }

        #endregion

        private void tabMain_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (e.Page.Name == xtabBenhSu.Name)
            {
                this.loadData(this.gdBenhSu, InfoFacility.TABLENAME, InfoFacility.FACNAME, InfoFacility.eFacilityType._eBenhSu);
            }
            else if (e.Page.Name == xtabDienTienBenh.Name)
            {
                this.loadData(this.gdDienTienBenh, InfoFacility.TABLENAME, InfoFacility.FACNAME, InfoFacility.eFacilityType._eDienTienBenh);
            }
            else if (e.Page.Name == xtabNgheNghiep.Name)
            {
                this.loadData(this.gdNgheNghiep, InfoFacility.TABLENAME, InfoFacility.FACNAME, InfoFacility.eFacilityType._eNgheNghiep);
            }
            else if (e.Page.Name == xtabTienCan.Name)
            {
                this.loadData(this.gdTienCan, InfoFacility.TABLENAME, InfoFacility.FACNAME, InfoFacility.eFacilityType._eTienCan);
            }
            else if (e.Page.Name == xtabTuongTrinhPT.Name)
            {
                this.loadData(this.gdTTPT, InfoSurgeryReport.TABLENAME, InfoSurgeryReport.RPTNAME, InfoFacility.eFacilityType._none);
            }
            else if (e.Page.Name == xtabThuThuat.Name)
            {
                this.loadData(this.gdThuThuat, InfoTreatmentSkill.TABLENAME, InfoTreatmentSkill.TRSSKILLNAME, InfoFacility.eFacilityType._none);
            }
            else if (e.Page.Name == xtabPhauThuat.Name)
            {
                this.loadData(this.gdPhauThuat, InfoSurgical.TABLENAME, InfoSurgical.SURNAME, InfoFacility.eFacilityType._none);
            }
            else if (e.Page.Name == xtabAdvice.Name)
            {
                this.loadData(this.gdLoiKhuyen, InfoAdvice.TABLENAME, InfoAdvice.ADVDOCTORADVICE, InfoFacility.eFacilityType._none);
            }
        }

    }
}